package com.hxh.generater.springBoot.springboot3_vue3.gen;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.hxh.entity.Project;
import com.hxh.entity.ProjectDatasource;
import com.hxh.entity.ProjectTableFiledConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.entity.po.FieldName;
import com.hxh.generater.common.GenSysMenu;
import com.hxh.generater.springBoot.genBase.GenTableMenu;
import com.hxh.generater.springBoot.springboot3_vue3.gen.crud.GenControllerRemote;
import com.hxh.util.GenUtil;
import com.hxh.util.MysqlUtils;

import java.util.Date;
import java.util.List;

public class GenSb3Vue3TableMenu implements GenTableMenu {

    private static final String TABLE_MENU_NAME = "sys_menu";

    @Override
    public void initTableMenu(Project project, ClassInfo classInfo) throws Exception {
        ProjectDatasource projectDatasource = project.getProjectDatasource();
        String mutilDatasourceStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, "", "/");
        String path = "/" + mutilDatasourceStr + StrUtil.toCamelCase(classInfo.getTableName());

        Object o = MysqlUtils.queryOneAndReturnFirstResult(projectDatasource, "select count(*) from sys_menu where path = '" + path + "';");
        if (o != null) {
            if (Integer.parseInt(o.toString()) > 0) {
                return;
            }
        }

        String mutilDatasourcePemStr = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, "", ":");
        GenSysMenu sysMenuInput = getSysMenuDirInput(classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        String sql = MysqlUtils.genInsertSql(sysMenuInput, TABLE_MENU_NAME);


        int id = MysqlUtils.executeInsertSqlWithId(projectDatasource, sql);

        //添加菜单
        GenSysMenu menu = getSysMenuDirInput(id, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(menu, TABLE_MENU_NAME);
        id = MysqlUtils.executeInsertSqlWithId(projectDatasource, sql);

        //查询按钮
        GenSysMenu queryMenu = getSysMenuQueryInput(id, menu, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(queryMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource, sql);


        //添加按钮
        GenSysMenu addMenu = getSysMenuAddInput(id, menu, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(addMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource, sql);

        //修改按钮
        GenSysMenu updateMenu = getSysMenuUpdateInput(id, menu, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(updateMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource, sql);

        //删除按钮
        GenSysMenu deleteMenu = getSysMenuDeleteInput(id, menu, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(deleteMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource, sql);

        GenSysMenu importMenu = getSysMenuImportInput(id, menu, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(importMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource, sql);

        GenSysMenu exportMenu = getSysMenuExportInput(id, menu, classInfo, mutilDatasourceStr, mutilDatasourcePemStr);
        sql = MysqlUtils.genInsertSql(exportMenu, TABLE_MENU_NAME);
        MysqlUtils.executeOneSql(projectDatasource, sql);


        //关联字段、
        List<FieldName> fieldNameList = classInfo.getFieldNameList();
        if (CollectionUtil.isEmpty(fieldNameList)) {
            return;
        }
        for (FieldName fn : fieldNameList) {
            ProjectTableFiledConfig projectTableFiledConfig = fn.getProjectTableFiledConfig();
            if (projectTableFiledConfig == null || !"association".equals(projectTableFiledConfig.getShowType())) {
                continue;
            }
            GenControllerRemote.processAssociationFnMenu(project, classInfo, projectTableFiledConfig);
        }
    }

    private static GenSysMenu getSysMenuDirInput(ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu sysMenuInput = new GenSysMenu();
        sysMenuInput.setParentId(0L);
        sysMenuInput.setTreePath("0");
        sysMenuInput.setName(classInfo.getDesc());
        sysMenuInput.setType("2");
        sysMenuInput.setPath("/" + mutilDatasourceStr + className);
        sysMenuInput.setComponent("Layout");

        sysMenuInput.setPerm(mutilDatasourcePemStr + className);
        sysMenuInput.setAlwaysShow("1");
        sysMenuInput.setKeepAlive("1");
        sysMenuInput.setVisible("1");
        sysMenuInput.setSort(0);
        sysMenuInput.setIcon("table");
        sysMenuInput.setCreateTime(new Date());
        sysMenuInput.setUpdateTime(new Date());
        return sysMenuInput;
    }


    private static GenSysMenu getSysMenuDirInput(int id, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu menu = new GenSysMenu();
        menu.setParentId((long) id);
        menu.setTreePath("0," + id);
        menu.setName(classInfo.getDesc() + "页面");
        menu.setType("1");
        menu.setPath("/" + mutilDatasourceStr + className + "/page");
        menu.setComponent(mutilDatasourceStr + className + "/index");
        menu.setPerm(mutilDatasourcePemStr + className + ":page");
        menu.setAlwaysShow("1");
        menu.setKeepAlive("1");
        menu.setVisible("1");
        menu.setSort(0);
        menu.setIcon("table");
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        return menu;
    }

    private static GenSysMenu getSysMenuDeleteInput(int id, GenSysMenu menu, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        GenSysMenu deleteMenu = new GenSysMenu();
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        deleteMenu.setParentId((long) id);
        deleteMenu.setTreePath(menu.getTreePath() + "," + id);
        deleteMenu.setName("删除" + classInfo.getDesc());
        deleteMenu.setType("4");
        deleteMenu.setPath("/api/" + mutilDatasourceStr + className + "/delete");
        deleteMenu.setPerm(mutilDatasourcePemStr + className + ":delete");
        deleteMenu.setAlwaysShow("1");
        deleteMenu.setKeepAlive("1");
        deleteMenu.setVisible("1");
        deleteMenu.setSort(0);
        deleteMenu.setCreateTime(new Date());
        deleteMenu.setUpdateTime(new Date());
        return deleteMenu;
    }

    private static GenSysMenu getSysMenuImportInput(int id, GenSysMenu menu, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        GenSysMenu deleteMenu = new GenSysMenu();
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        deleteMenu.setParentId((long) id);
        deleteMenu.setTreePath(menu.getTreePath() + "," + id);
        deleteMenu.setName("批量导入");
        deleteMenu.setType("4");
        deleteMenu.setPath("/api/" + mutilDatasourceStr + className + "/batchImport");
        deleteMenu.setPerm(mutilDatasourcePemStr + className + ":batchImport");
        deleteMenu.setAlwaysShow("1");
        deleteMenu.setKeepAlive("1");
        deleteMenu.setVisible("1");
        deleteMenu.setSort(0);
        deleteMenu.setCreateTime(new Date());
        deleteMenu.setUpdateTime(new Date());
        return deleteMenu;
    }

    private static GenSysMenu getSysMenuExportInput(int id, GenSysMenu menu, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        GenSysMenu deleteMenu = new GenSysMenu();
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        deleteMenu.setParentId((long) id);
        deleteMenu.setTreePath(menu.getTreePath() + "," + id);
        deleteMenu.setName("批量导出");
        deleteMenu.setType("4");
        deleteMenu.setPath("/api/" + mutilDatasourceStr + className + "/batchExport");
        deleteMenu.setPerm(mutilDatasourcePemStr + className + ":batchExport");
        deleteMenu.setAlwaysShow("1");
        deleteMenu.setKeepAlive("1");
        deleteMenu.setVisible("1");
        deleteMenu.setSort(0);
        deleteMenu.setCreateTime(new Date());
        deleteMenu.setUpdateTime(new Date());
        return deleteMenu;
    }

    private static GenSysMenu getSysMenuUpdateInput(int id, GenSysMenu menu, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu updateMenu = new GenSysMenu();
        updateMenu.setParentId((long) id);
        updateMenu.setTreePath(menu.getTreePath() + "," + id);
        updateMenu.setName("修改" + classInfo.getDesc());
        updateMenu.setType("4");
        updateMenu.setPath("/api/" + mutilDatasourceStr + className + "/edit");
        updateMenu.setPerm(mutilDatasourcePemStr + className + ":edit");
        updateMenu.setAlwaysShow("1");
        updateMenu.setKeepAlive("1");
        updateMenu.setVisible("1");
        updateMenu.setSort(0);
        updateMenu.setCreateTime(new Date());
        updateMenu.setUpdateTime(new Date());
        return updateMenu;
    }

    private static GenSysMenu getSysMenuAddInput(int id, GenSysMenu menu, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu addMenu = new GenSysMenu();
        addMenu.setParentId((long) id);
        addMenu.setTreePath(menu.getTreePath() + "," + id);
        addMenu.setName("添加" + classInfo.getDesc());
        addMenu.setType("4");
        addMenu.setPath("/api/" + mutilDatasourceStr + className + "/add");
        addMenu.setPerm(mutilDatasourcePemStr + className + ":add");
        addMenu.setAlwaysShow("1");
        addMenu.setKeepAlive("1");
        addMenu.setVisible("1");
        addMenu.setSort(0);
        addMenu.setCreateTime(new Date());
        addMenu.setUpdateTime(new Date());
        return addMenu;
    }

    private static GenSysMenu getSysMenuQueryInput(int id, GenSysMenu menu, ClassInfo classInfo, String mutilDatasourceStr, String mutilDatasourcePemStr) {
        String className = classInfo.getClassName();
        className = StrUtil.lowerFirst(className);
        GenSysMenu queryMenu = new GenSysMenu();
        queryMenu.setParentId((long) id);
        queryMenu.setTreePath(menu.getTreePath() + "," + id);
        queryMenu.setName("查询" + classInfo.getDesc());
        queryMenu.setType("4");
        queryMenu.setPath("/api/" + mutilDatasourceStr + className + "/query");
        queryMenu.setPerm(mutilDatasourcePemStr + className + ":query");
        queryMenu.setAlwaysShow("1");
        queryMenu.setKeepAlive("1");
        queryMenu.setVisible("1");
        queryMenu.setSort(0);
        queryMenu.setCreateTime(new Date());
        queryMenu.setUpdateTime(new Date());
        return queryMenu;
    }
}
